package com.example.administrator.familfinalcialmanagement.ui.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TimePicker;
import android.widget.Toast;

import com.example.administrator.familfinalcialmanagement.R;
import com.example.administrator.familfinalcialmanagement.bean.GreenDaoUtils;
import com.example.administrator.familfinalcialmanagement.bean.ToastBean;
import com.example.administrator.familfinalcialmanagement.greendao.gen.ToastBeanDao;
import com.example.administrator.familfinalcialmanagement.ui.BaseActivity;
import com.example.administrator.familfinalcialmanagement.util.LogUtil;
import com.example.administrator.familfinalcialmanagement.util.ToastUtil;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import static com.example.administrator.familfinalcialmanagement.GRApplication.instance;

public class AddToastActivity extends BaseActivity {
    ToastBeanDao toastBeanDao = GreenDaoUtils.getSingleTon().getmDaoSession().getToastBeanDao();
    private EditText ed_title, editText;
    private Spinner spinner;
    private Button btn;
    private final String[] spinnerItems = {"闹钟提醒", "短信提醒", "通知提醒"};
    private int tag;
    private DatePicker datePicker;
    private TimePicker timePicker;
    private String time;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_toast);
        init();
    }

    private void init() {
        setBack();
        setTitleText("创建备忘提醒");
        ed_title = findViewById(R.id.ed_title);
        editText = findViewById(R.id.ed_text);
        spinner = findViewById(R.id.spinner);
        btn = findViewById(R.id.btn_yes);
        datePicker = (DatePicker) findViewById(R.id.dpPicker);
        timePicker = (TimePicker) findViewById(R.id.tpPicker);
        //
        ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<>(instance,
                android.R.layout.simple_spinner_item, spinnerItems);
        spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(spinnerAdapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                tag = pos;

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //保存
                setDB();
            }
        });
        //初始化日期控件
        datePicker.init(2013, 8, 20, new DatePicker.OnDateChangedListener() {

            @Override
            public void onDateChanged(DatePicker view, int year,
                                      int monthOfYear, int dayOfMonth) {
                // 获取一个日历对象，并初始化为当前选中的时间
                Calendar calendar = Calendar.getInstance();
                calendar.set(year, monthOfYear, dayOfMonth);
                SimpleDateFormat format = new SimpleDateFormat(
                        "yyyy年MM月dd日  HH:mm");
                time=format.format(calendar.getTime());

            }
        });

        timePicker.setIs24HourView(true);
        timePicker
                .setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
                    @Override
                    public void onTimeChanged(TimePicker view, int hourOfDay,
                                              int minute) {
//                        Toast.makeText(DataTimePicker.this,
//                                hourOfDay + "小时" + minute + "分钟",
//                                Toast.LENGTH_SHORT).show();
                    }
                });

    }


    private void setDB() {
        ToastBean bean = new ToastBean();
        bean.setTitle(ed_title.getText().toString());
        bean.setInfo(editText.getText().toString());
        bean.setToastStyle(spinnerItems[tag]);
        bean.setToastTime(time);
        //插入
        toastBeanDao.insert(bean);
        Toast.makeText(AddToastActivity.this, "创建成功!", Toast.LENGTH_SHORT).show();
    }

}
